﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MtgPrinter
{
    public partial class SettingsForm : Form
    {
        MtgPrinterConfiguration _appConfig;
        public MtgPrinterConfiguration Config
        {
            protected get {return _appConfig;}
            set 
            { 
                _appConfig = value;
                InitializeFieldsWithConfig(_appConfig);
            }
        }

        public SettingsForm(MtgPrinterConfiguration config)
        {
            InitializeComponent();
            Config = config;
        }

        public SettingsForm()
        {
            InitializeComponent();
           
        }

        private void InitializeFieldsWithConfig(MtgPrinterConfiguration config) 
        {
            MtgPrinterConfiguration defconf = new MtgPrinterConfiguration();
            nudWidth.Value = config.CardWidthCM;
            nudHeight.Value = config.CardHeightCM;
            nudRatio.Value = config.CardAspectRatio;
            cbRatio.Checked = config.CardMaintainRatio;
            nudThreads.Value = config.NumberOfDownloadingThreads;

            if(config.RemoveFromCacheAfter>0)
                nudRemoveAfter.Value = config.RemoveFromCacheAfter;
            else 
                nudRemoveAfter.Value = defconf.RemoveFromCacheAfter;

            nudMaxCacheSize.Value = (int)config.MaxCacheSize/1024/1024;

            cbAllowCache.Checked = config.AllowDiskCaching;

            radioButton1.Checked= (config.DownloadPicturesFrom == MtgLib.EDownloadPicturesFrom.MagicCards_info);
            radioButton2.Checked = (config.DownloadPicturesFrom == MtgLib.EDownloadPicturesFrom.MtgVault_com);

            switch (config.PdfPageSize)
            {
                case EExportPdfPageSize.Legal:
                    this.cbPageSize.SelectedIndex = 2;
                    break;

                case EExportPdfPageSize.Letter:
                    this.cbPageSize.SelectedIndex = 1;
                    break;
                case EExportPdfPageSize.A4:
                default:
                    this.cbPageSize.SelectedIndex = 0;
                    break;
            }

        }

        private void ParseForm()
        {
            decimal width = nudWidth.Value;
            decimal height = nudHeight.Value;
            decimal ratio = nudRatio.Value;
            bool bmaint = cbRatio.Checked;
            int numTreads = (int)nudThreads.Value;

            Config.CardAspectRatio = ratio;
            Config.CardMaintainRatio = bmaint;
            Config.CardWidthCM = width;
            Config.CardHeightCM = height;
            Config.NumberOfDownloadingThreads = numTreads;

            Config.RemoveFromCacheAfter = (int)nudRemoveAfter.Value;
            Config.MaxCacheSize = (int)nudMaxCacheSize.Value * 1024 * 1024;
            Config.AllowDiskCaching = cbAllowCache.Checked;

            if (radioButton1.Checked) Config.DownloadPicturesFrom = MtgLib.EDownloadPicturesFrom.MagicCards_info;
            else if (radioButton2.Checked) Config.DownloadPicturesFrom = MtgLib.EDownloadPicturesFrom.MtgVault_com;

                if(this.cbPageSize.SelectedIndex==0)
                {
                    Config.PdfPageSize = EExportPdfPageSize.A4;
                }
                else if (this.cbPageSize.SelectedIndex==1)
                {
                    Config.PdfPageSize = EExportPdfPageSize.Letter;
                }
                else if (this.cbPageSize.SelectedIndex==2)
                {
                    Config.PdfPageSize = EExportPdfPageSize.Legal;
                }
            
        }

        private void SettingsForm_Load(object sender, EventArgs e)
        {

        }

        private void nudWidth_ValueChanged(object sender, EventArgs e)
        {
            if (cbRatio.Checked)
            {
                decimal targetVal = nudWidth.Value / nudRatio.Value;
                if (targetVal > nudHeight.Maximum) nudHeight.Value = nudHeight.Maximum;
                else if (targetVal < nudHeight.Minimum) nudHeight.Value = nudHeight.Minimum;
                else nudHeight.Value = targetVal;

            }
        }

        private void nudHeight_ValueChanged(object sender, EventArgs e)
        {
            if (cbRatio.Checked)
            {
                decimal targetVal = nudHeight.Value * nudRatio.Value;
                if (targetVal > nudWidth.Maximum) nudWidth.Value = nudWidth.Maximum;
                else if (targetVal < nudWidth.Minimum) nudWidth.Value = nudWidth.Minimum;
                else nudWidth.Value = targetVal;
            }
        }

        private void nudRatio_ValueChanged(object sender, EventArgs e)
        {
            if (cbRatio.Checked)
            {
                decimal targetVal = nudWidth.Value / nudRatio.Value;
                if (targetVal > nudHeight.Maximum) nudHeight.Value = nudHeight.Maximum;
                else if (targetVal < nudHeight.Minimum) nudHeight.Value = nudHeight.Minimum;
                else nudHeight.Value = targetVal;

                targetVal = nudHeight.Value * nudRatio.Value;
                if (targetVal > nudWidth.Maximum) nudWidth.Value = nudWidth.Maximum;
                else if (targetVal < nudWidth.Minimum) nudWidth.Value = nudWidth.Minimum;
                else nudWidth.Value = targetVal;
            }
        }

        private void bOK_Click(object sender, EventArgs e)
        {
            ParseForm();
            this.Close();
        }

        private void bCancel_Click(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            DialogResult result= MessageBox.Show("Are you sure?", "Reset to Defaults", MessageBoxButtons.YesNo, MessageBoxIcon.Asterisk);

            if (result == System.Windows.Forms.DialogResult.Yes)
            {
                InitializeFieldsWithConfig(new MtgPrinterConfiguration());
            }

        }

        private void cbAllowCache_CheckedChanged(object sender, EventArgs e)
        {
            if (cbAllowCache.Checked)
            {
                nudRemoveAfter.Enabled = true;
                nudMaxCacheSize.Enabled = true;
            }
            else
            {
                nudRemoveAfter.Enabled = false;
                nudMaxCacheSize.Enabled = false;
            }

        }
    }
}
